<template>
  <div class="banner">
    <div class="bg">
      <img :src="activeFilm.poster" alt="" />
    </div>

    <!-- swiper 容器 -->
    <div ref="swiperRef" class="swiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="item in films" :key="item.filmId">
          <img class="img" :src="item.poster" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, computed } from "vue";
// 引入 swiper
import Swiper from "swiper";
import "swiper/css";

const props = defineProps<{
  films: IFilm[];
  activeIndex: number;
}>();

const emit = defineEmits(["update:activeIndex"]);

// 当前激活的电影
const activeFilm = computed(() => {
  return props.films[props.activeIndex];
});

const swiperRef = ref<HTMLDivElement | null>(null);
// 初始化 swiper
const initSwiper = () => {
  // console.log(document.querySelector(".swiper-wrapper")?.innerHTML, "===");
  // 实例对象
  const swiper = new Swiper(swiperRef.value!, {
    initialSlide: props.activeIndex,
    slidesPerView: 4,
    spaceBetween: 16,
    centeredSlides: true,
    slideToClickedSlide: true,
  });

  swiper.on("slideChange", () => {
    console.log("切换激活下标");
    emit("update:activeIndex", swiper.activeIndex);
  });
};

onMounted(() => {
  initSwiper();
});
</script>

<style lang="scss" scoped>
.banner {
  position: relative;
  height: 160px;
}

.bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;

  img {
    filter: blur(30px);
  }
}

.swiper {
  height: 160px;
  padding: 15px 0;
  box-sizing: border-box;
  // background-color: green;
}

.swiper-slide {
  display: flex;
  justify-content: center;
  align-items: flex-end;

  .img {
    display: block;
    // width: 72px;
    width: 72px;
    height: 104px;
  }

  &.swiper-slide-active .img {
    width: 100%;
    height: 100%;
  }
}
</style>
